//
//  CaipiaoManager.m
//  EasyToo
//
//  Created by linshengqin on 13-8-25.
//
//

#import "CaipiaoManager.h"
#import "ServerCommunicationManager.h"
#import "CaiPiaoQiCiRequest.h"
#import "CaiPiaoChargeRequest.h"
#import "CaiPiaoOrderRequest.h"
#import "CaiPiaoQiCiResponse.h"
#import "CaiPiaoOrderRequest.h"
#import "CaiPiaoOrderResponse.h"

@implementation CaipiaoManager

static CaipiaoManager* gManager = nil;

+ (CaipiaoManager *)shareManager
{
    if (gManager == nil) {
        gManager = [[CaipiaoManager alloc] init];
    }
    return gManager;
}

- (void)CaiPiaoQiCi:(NSString*)type Qici:(NSString*)qici
{
    CaiPiaoQiCiRequest *request = [[CaiPiaoQiCiRequest alloc] initWithType:type Qici:qici Delegate:self];
   [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

- (void)CaiPiaoCharge:(NSString*)money CardID:(NSString*)cardId
{
    CaiPiaoChargeRequest *request = [[CaiPiaoChargeRequest alloc] initWithMoney:money cardID:cardId Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

-(void)caipiaoOrder:(NSMutableArray *)stake
              BType:(NSString *)bType
           BetCount:(NSString *)betCount
           BetMoney:(NSString *)betMoney
                 BS:(NSString *)bs
           isAppend:(NSString *)isAppend
              Issue:(NSString *)issue
              LType:(NSString *)ltype
              PType:(NSString *)ptype
            allMoney:(NSString *)totalPrice
             CardID:(NSString *)cardID
{
    CaiPiaoOrderRequest *request = [[CaiPiaoOrderRequest alloc]initWithStake:stake BType:bType BetCount:betCount BetMoney:betMoney BS:bs isAppend:isAppend Issue:issue LType:ltype PType:ptype allMoney:totalPrice CardID:cardID Delegate:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}

- (void)onRequestSuccess:(BaseResponse*)response
{
    if ([response isKindOfClass:[CaiPiaoQiCiResponse class]])
    {
        [self handleCaiPiaoQiCiSearchSuccess:(CaiPiaoQiCiResponse*)response];
    }
    else if ([response isKindOfClass:[CaiPiaoOrderResponse class]])
    {
        [self handleCaiPiaoOrderSuccess:(CaiPiaoOrderResponse*)response];
    }
}


- (void)onRequestFail:(BaseResponse*)response
{
    
}

- (void)handleCaiPiaoQiCiSearchSuccess:(CaiPiaoQiCiResponse*)response
{
    NSMutableArray* qiciInfoArray = response.qiciArray;
    if (qiciInfoArray)
    {
        [self.delegate onSearchQiCiFinish:YES QiCi:qiciInfoArray];
    }
    else
    {
        [self.delegate onSearchQiCiFinish:YES QiCi:nil];
    }
}

- (void)handleCaiPiaoOrderSuccess:(CaiPiaoOrderResponse*)response
{
    NSString *tn = response.tn;
    
    if (tn)
    {
        [self.delegate onOrderCaipiaoFinish:YES Tn:tn];
    }
    else
    {
        [self.delegate onOrderCaipiaoFinish:NO Tn:nil];
    }
}

@end
